package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.educational.entity.CourseTemplate;
import com.yuyou.fn.educational.entity.Product;
import com.yuyou.fn.educational.mapper.ProductMapper;
import com.yuyou.fn.educational.service.ICourseTemplateService;
import com.yuyou.fn.educational.service.IProductService;
import com.yuyou.fn.educational.vo.ProductQueryVo;
import com.yuyou.fn.educational.vo.ProductSaveOrUpdateVo;
import com.yuyou.fn.platform.permission.DataPermission;
import com.yuyou.fn.platform.service.IAreaTeamService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * Product 表数据服务层接口实现类
 *
 */
@Service
public class ProductServiceImpl extends SuperServiceImpl<ProductMapper, Product,Long> implements IProductService {

    @Resource
    private IAreaTeamService areaTeamService;

    @Resource
    private ICourseTemplateService courseTemplateService ;

    @Override
    public List<Product> findBaseProductForUse(DataPermission dataPermission) {
        Example<Product> example = Product.createExample();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillConditionForAreaTeamPermission(criteria, dataPermission.getAreaTeamIds());
        example.selectProperties("name") ;
        return findListByExample(example);
    }

    @Override
    public List<Product> findBaseProductByAreaTeamId(Long areaTeamId) {
        Example<Product> example = Product.createExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("areaTeamId",areaTeamId );
        example.selectProperties("name") ;
        return findListByExample(example);
    }

    @Override
    public Page<Product> findProductByQueryForManage(ProductQueryVo productQueryVo, DataPermission dataPermission, int pageNo, int pageSize) {
        Example<Product> example = Product.createExample();
        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria, productQueryVo);
        if(dataPermission.getUserType()  == 0 || dataPermission.getUserType() == 1 ){
            CriteriaUtils.fillConditionForAreaTeamPermission(criteria ,dataPermission.getAreaTeamIds());
        }else{
            criteria.andCondition(" 1 <> 1 ");
        }

        return findPageByExample(example, pageNo, pageSize);
    }

    @Override
    public Page<Product> findProductByQueryForUse(ProductQueryVo productQueryVo, DataPermission dataPermission, int pageNo, int pageSize) {
        Example<Product> example = Product.createExample();
        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria, productQueryVo);
        CriteriaUtils.fillConditionForAreaTeamPermission(criteria ,dataPermission.getAreaTeamIds());

        return findPageByExample(example, pageNo, pageSize);
    }

    @Override
    public void saveOrUpdateProduct(ProductSaveOrUpdateVo productSaveOrUpdateVo) {
        Product product = new Product();

        BeanUtils.copyProperties(product, productSaveOrUpdateVo);
        fillExtraData(product);
        checkProduct(product);
        if (product.getProductId() == null) {
            product.setCreateTime(TimeUtils.now());
            product.setUpdateTime(TimeUtils.now());
            insertSelective(product);
        } else {
            product.setUpdateTime(TimeUtils.now());
            updateSelectiveById(product);
        }
    }

    private void checkProduct(Product product) {
        Example<Product> example = Product.createExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("name" , product.getName()).andEqualTo("areaTeamId" , product.getAreaTeamId());

        Product one = findOneByExample(example);

        if(one != null && ( product.getProductId() == null || !one.getProductId().equals(product.getProductId()))){
            throw new BusException("产品名重复");
        }
    }

    private void fillExtraData(Product product) {

        if (product.getAreaTeamId() != null) {
            product.setAreaTeamName(areaTeamService.findTeamNameById(product.getAreaTeamId()));
        }

    }

    @Override
    public void changeProductStatus(Long productId, Integer status) {

        if (status != 0 && status != 1) {
            throw new IllegalArgumentException("Invalid status");
        }

        Product product = new Product();
        product.setProductId(productId);
        updateSelectiveById(product);

    }

    @Override
    public String findProductNameById(Long productId) {

        Example<Product> example = Product.createExample();
        example.selectProperties("name");
        example.createCriteria().andEqualTo("productId",productId);
        Product product = findOneByExample(example);

        return product == null ? StringUtils.EMPTY : product.getName() ;
    }

    @Override
    public Product findProductByNameAreaTeamId(String productName, Long areaTeamId) {
        Example<Product> example = Product.createExample();
        example.createCriteria().andEqualTo("areaTeamId",areaTeamId).andEqualTo("name",productName );
        return findOneByExample(example);
    }

    @Override
    public void deleteProduct(Long productId) {
        Example<CourseTemplate> courseTemplateExample = CourseTemplate.createExample();
        courseTemplateExample.createCriteria().andEqualTo("productId",productId);
        long l = courseTemplateService.countByExample(courseTemplateExample);
        if(l > 0 ){
            throw new BusException("产品已经使用无法删除");
        }
        deleteById(productId);
    }
}